package cn.liangent.travlib;

public class ResourceData {
	private static int MILLISECONDS_IN_HOUR = 60 * 60 * 1000;
	// change after construction only by calculateProduct
	private int lumber, clay, iron, crop;

	public ResourceData(int lumber, int clay, int iron, int crop) {
		this.lumber = lumber;
		this.clay = clay;
		this.iron = iron;
		this.crop = crop;
	}

	public int getLumber() {
		return lumber;
	}

	public int getClay() {
		return clay;
	}

	public int getIron() {
		return iron;
	}

	public int getCrop() {
		return crop;
	}
	
	// this will be invoked by CompleteResourceData by super.xxx
	public ResourceData calculateProduct(long time, CompleteResourceData production, ResourceData warehouse) {
		if (time <= 0) return this;
		ResourceData retv = new CompleteResourceData(
			(int)(lumber + production.getLumber() * time / MILLISECONDS_IN_HOUR),
			(int)(clay + production.getClay() * time / MILLISECONDS_IN_HOUR),
			(int)(iron + production.getIron() * time / MILLISECONDS_IN_HOUR),
			(int)(crop + (production.getCrop() - production.getCropConsumption())
				* time / MILLISECONDS_IN_HOUR),
			0
		);
		if (retv.crop < 0) {
			retv.crop = 0;
		}
		if (retv.lumber > warehouse.lumber) {
			retv.lumber = warehouse.lumber;
		}
		if (retv.clay > warehouse.clay) {
			retv.clay = warehouse.clay;
		}
		if (retv.iron > warehouse.iron) {
			retv.iron = warehouse.iron;
		}
		if (retv.crop > warehouse.crop) {
			retv.crop = warehouse.crop;
		}
		return retv;
	}
}
